home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Comunicatii / htttrack / httrack-3.32-2.exe / {app} / src / htsbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-08  |  10.3 KB  |  393 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Basic definitions                                      */
  34. /*       Used in .c files for basic (malloc() ..) definitions   */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #ifndef HTS_BASICH
  39. #define HTS_BASICH
  40.  
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44.  
  45. #include "htsglobal.h"
  46.  
  47. // size_t et mode_t
  48. #ifndef _WIN32_WCE
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #else
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include "cecompat.h"
  55. #endif
  56. #include <string.h>
  57. #include <time.h>
  58.  
  59. #ifdef HAVE_UNISTD_H
  60. #include <unistd.h>
  61. #endif
  62. #ifdef HAVE_SYS_TYPES_H
  63. #include <sys/types.h>
  64. #endif
  65. #ifdef HAVE_SYS_STAT_H
  66. #include <sys/stat.h>
  67. #endif
  68. #ifdef HAVE_DLFCN_H
  69. #include <dlfcn.h>
  70. #endif
  71.  
  72. #ifndef _WIN32
  73. #include <errno.h>
  74. #endif
  75.  
  76. #if HTS_WIN
  77. #else
  78. #include <fcntl.h>
  79. #endif
  80. #include <assert.h>
  81.  
  82. #undef min
  83. #undef max
  84. #define min(a,b) ((a)>(b)?(b):(a))
  85. #define max(a,b) ((a)>(b)?(a):(b))
  86.  
  87. // teste ΘgalitΘ de 2 chars, case insensitive
  88. #define hichar(a) ((((a)>='a') && ((a)<='z')) ? ((a)-('a'-'A')) : (a))
  89. #define streql(a,b) (hichar(a)==hichar(b))
  90.  
  91. // caractΦre maj
  92. #define isUpperLetter(a) ( ((a) >= 'A') && ((a) <= 'Z') )
  93.  
  94.  
  95. /* Library internal definictions */
  96. #ifdef HTS_INTERNAL_BYTECODE
  97.  
  98.  
  99. // functions
  100. #ifdef _WIN32
  101. #define DynamicGet(handle, sym) GetProcAddress(handle, sym)
  102. #else
  103. #define DynamicGet(handle, sym) dlsym(handle, sym)
  104. #endif
  105.  
  106. // emergency log
  107. typedef void (*t_abortLog)(char* msg, char* file, int line);
  108. extern HTSEXT_API t_abortLog abortLog__;
  109. #define abortLog(a) abortLog__(a, __FILE__, __LINE__)
  110. #define abortLogFmt(a) do { \
  111.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  112.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  113.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  114.   if (!fp) fp = fopen("\\Temp\\CRASH.TXT", "wb"); \
  115.   if (!fp) fp = fopen("\\CRASH.TXT", "wb"); \
  116.   if (!fp) fp = fopen("CRASH.TXT", "wb"); \
  117.   if (fp) { \
  118.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  119.     fprintf(fp, "Reason:\r\n"); \
  120.     fprintf(fp, a); \
  121.     fprintf(fp, "\r\n"); \
  122.     fflush(fp); \
  123.     fclose(fp); \
  124.   } \
  125. } while(0)
  126.  
  127.  
  128. #define _ ,
  129. #define abortLogFmt(a) do { \
  130.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  131.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  132.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  133.   if (!fp) fp = fopen("\\Temp\\CRASH.TXT", "wb"); \
  134.   if (!fp) fp = fopen("\\CRASH.TXT", "wb"); \
  135.   if (!fp) fp = fopen("CRASH.TXT", "wb"); \
  136.   if (fp) { \
  137.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  138.     fprintf(fp, "Reason:\r\n"); \
  139.     fprintf(fp, a); \
  140.     fprintf(fp, "\r\n"); \
  141.     fflush(fp); \
  142.     fclose(fp); \
  143.   } \
  144. } while(0)
  145. #define assertf(exp) do { \
  146.   if (! ( exp ) ) { \
  147.     abortLog("assert failed: " #exp); \
  148.     if (htsCallbackErr != NULL) { \
  149.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  150.     } \
  151.     assert(exp); \
  152.     abort(); \
  153.   } \
  154. } while(0)
  155. /* non-fatal assert */
  156. #define assertnf(exp) do { \
  157.   if (! ( exp ) ) { \
  158.     abortLog("assert failed: " #exp); \
  159.     if (htsCallbackErr != NULL) { \
  160.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  161.     } \
  162.   } \
  163. } while(0)
  164.  
  165.  
  166. /* regular malloc's() */
  167. #ifndef HTS_TRACE_MALLOC
  168. #define malloct(A)          malloc(A)
  169. #define calloct(A,B)        calloc((A), (B))
  170. #define freet(A)            do { assertnf((A) != NULL); if ((A) != NULL) { free(A); (A) = NULL; } } while(0)
  171. #define strdupt(A)          strdup(A)
  172. #define realloct(A,B)       ( ((A) != NULL) ? realloc((A), (B)) : malloc(B) )
  173. #define memcpybuff(A, B, N) memcpy((A), (B), (N))
  174. #else
  175. /* debug version */
  176. #define malloct(A)    hts_malloc(A)
  177. #define calloct(A,B)  hts_calloc(A,B)
  178. #define strdupt(A)    hts_strdup(A)
  179. #define freet(A)      do { hts_free(A); (A) = NULL; } while(0)
  180. #define realloct(A,B) hts_realloc(A,B)
  181. void  hts_freeall();
  182. void* hts_malloc    (size_t);
  183. void* hts_calloc(size_t,size_t);
  184. char* hts_strdup(char*);
  185. void* hts_xmalloc(size_t,size_t);
  186. void  hts_free      (void*);
  187. void* hts_realloc   (void*,size_t);
  188. mlink* hts_find(char* adr);
  189. /* protected memcpy */
  190. #define memcpybuff(A, B, N) do { \
  191.   mlink* lnk = hts_find((void*)(A)); \
  192.   if (lnk != NULL) { \
  193.     assertf(lnk != NULL); \
  194.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) - sizeof(htsboundary) ) ) == htsboundary ); \
  195.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) + lnk->len ) ) == htsboundary ); \
  196.     assertf( ( ((char*)(A)) + (N)) < (char*) (lnk->adr + lnk->len) ); \
  197.   } \
  198.   memcpy(A, B, N); \
  199. } while(0)
  200.  
  201. #endif
  202.  
  203. typedef void (* htsErrorCallback)(char* msg, char* file, int line);
  204. extern HTSEXT_API htsErrorCallback htsCallbackErr;
  205. extern HTSEXT_API int htsMemoryFastXfr;
  206.  
  207. /*
  208. */
  209.  
  210.  
  211. #ifdef STRDEBUG
  212.  
  213. /* protected strcat, strncat and strcpy - definitely useful */
  214. #define strcatbuff(A, B) do { \
  215.   assertf( (A) != NULL ); \
  216.   if ( ! (B) ) { assertf( 0 ); } \
  217.   if (htsMemoryFastXfr) { \
  218.     if (sizeof(A) != sizeof(char*)) { \
  219.       (A)[sizeof(A) - 1] = '\0'; \
  220.     } \
  221.     strcat(A, B); \
  222.     if (sizeof(A) != sizeof(char*)) { \
  223.       assertf((A)[sizeof(A) - 1] == '\0'); \
  224.     } \
  225.   } else { \
  226.     unsigned int sz = (unsigned int) strlen(A); \
  227.     unsigned int szf = (unsigned int) strlen(B); \
  228.     if (sizeof(A) != sizeof(char*)) { \
  229.       assertf(sz + szf + 1 < sizeof(A)); \
  230.       if (szf > 0) { \
  231.         if (sz + szf + 1 < sizeof(A)) { \
  232.           memcpy((A) + sz, (B), szf + 1); \
  233.         } \
  234.       } \
  235.     } else if (szf > 0) { \
  236.       memcpybuff((A) + sz, (B), szf + 1); \
  237.     } \
  238.   } \
  239. } while(0)
  240. #define strncatbuff(A, B, N) do { \
  241.   assertf( (A) != NULL ); \
  242.   if ( ! (B) ) { assertf( 0 ); } \
  243.   if (htsMemoryFastXfr) { \
  244.     if (sizeof(A) != sizeof(char*)) { \
  245.       (A)[sizeof(A) - 1] = '\0'; \
  246.     } \
  247.     strncat(A, B, N); \
  248.     if (sizeof(A) != sizeof(char*)) { \
  249.       assertf((A)[sizeof(A) - 1] == '\0'); \
  250.     } \
  251.   } else { \
  252.     unsigned int sz = (unsigned int) strlen(A); \
  253.     unsigned int szf = (unsigned int) strlen(B); \
  254.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  255.     if (sizeof(A) != sizeof(char*)) { \
  256.       assertf(sz + szf + 1 < sizeof(A)); \
  257.       if (szf > 0) { \
  258.         if (sz + szf + 1 < sizeof(A)) { \
  259.           memcpy((A) + sz, (B), szf); \
  260.           * ( (A) + sz + szf) = '\0'; \
  261.         } \
  262.       } \
  263.     } else if (szf > 0) { \
  264.       memcpybuff((A) + sz, (B), szf); \
  265.       * ( (A) + sz + szf) = '\0'; \
  266.     } \
  267.   } \
  268. } while(0)
  269. #define strcpybuff(A, B) do { \
  270.   assertf( (A) != NULL ); \
  271.   if ( ! (B) ) { assertf( 0 ); } \
  272.   if (htsMemoryFastXfr) { \
  273.     if (sizeof(A) != sizeof(char*)) { \
  274.       (A)[sizeof(A) - 1] = '\0'; \
  275.     } \
  276.     strcpy(A, B); \
  277.     if (sizeof(A) != sizeof(char*)) { \
  278.       assertf((A)[sizeof(A) - 1] == '\0'); \
  279.     } \
  280.   } else { \
  281.     unsigned int szf = (unsigned int) strlen(B); \
  282.     if (sizeof(A) != sizeof(char*)) { \
  283.       assertf(szf + 1 < sizeof(A)); \
  284.       if (szf > 0) { \
  285.         if (szf + 1 < sizeof(A)) { \
  286.           memcpy((A), (B), szf + 1); \
  287.         } else { \
  288.           * (A) = '\0'; \
  289.         } \
  290.       } else { \
  291.         * (A) = '\0'; \
  292.       } \
  293.     } else { \
  294.       memcpybuff((A), (B), szf + 1); \
  295.     } \
  296.   } \
  297. } while(0)
  298. #define strncpybuff(A, B, N) do { \
  299.   assertf( (A) != NULL ); \
  300.   if ( ! (B) ) { assertf( 0 ); } \
  301.   if (htsMemoryFastXfr) { \
  302.     if (sizeof(A) != sizeof(char*)) { \
  303.       (A)[sizeof(A) - 1] = '\0'; \
  304.     } \
  305.     strncpy(A, B, N); \
  306.     if (sizeof(A) != sizeof(char*)) { \
  307.       assertf((A)[sizeof(A) - 1] == '\0'); \
  308.     } \
  309.   } else { \
  310.     unsigned int szf = (unsigned int) strlen(B); \
  311.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  312.     if (sizeof(A) != sizeof(char*)) { \
  313.       assertf(szf + 1 < sizeof(A)); \
  314.       if (szf > 0) { \
  315.         if (szf + 1 < sizeof(A)) { \
  316.           memcpy((A), (B), szf); \
  317.         } \
  318.       } \
  319.     } else { \
  320.       memcpybuff((A), (B), szf); \
  321.     } \
  322.   } \
  323. } while(0)
  324.  
  325. #else
  326.  
  327. #ifdef STRDEBUGFAST
  328.  
  329. /* protected strcat, strncat and strcpy - definitely useful */
  330. #define strcatbuff(A, B) do { \
  331.   assertf( (A) != NULL ); \
  332.   if ( ! (B) ) { assertf( 0 ); } \
  333.   if (sizeof(A) != sizeof(char*)) { \
  334.     (A)[sizeof(A) - 1] = '\0'; \
  335.   } \
  336.   strcat(A, B); \
  337.   if (sizeof(A) != sizeof(char*)) { \
  338.     assertf((A)[sizeof(A) - 1] == '\0'); \
  339.   } \
  340. } while(0)
  341. #define strncatbuff(A, B, N) do { \
  342.   assertf( (A) != NULL ); \
  343.   if ( ! (B) ) { assertf( 0 ); } \
  344.   if (sizeof(A) != sizeof(char*)) { \
  345.     (A)[sizeof(A) - 1] = '\0'; \
  346.   } \
  347.   strncat(A, B, N); \
  348.   if (sizeof(A) != sizeof(char*)) { \
  349.     assertf((A)[sizeof(A) - 1] == '\0'); \
  350.   } \
  351. } while(0)
  352. #define strcpybuff(A, B) do { \
  353.   assertf( (A) != NULL ); \
  354.   if ( ! (B) ) { assertf( 0 ); } \
  355.   if (sizeof(A) != sizeof(char*)) { \
  356.     (A)[sizeof(A) - 1] = '\0'; \
  357.   } \
  358.   strcpy(A, B); \
  359.   if (sizeof(A) != sizeof(char*)) { \
  360.     assertf((A)[sizeof(A) - 1] == '\0'); \
  361.   } \
  362. } while(0)
  363. #define strncpybuff(A, B, N) do { \
  364.   assertf( (A) != NULL ); \
  365.   if ( ! (B) ) { assertf( 0 ); } \
  366.   if (sizeof(A) != sizeof(char*)) { \
  367.     (A)[sizeof(A) - 1] = '\0'; \
  368.   } \
  369.   strncpy(A, B, N); \
  370.   if (sizeof(A) != sizeof(char*)) { \
  371.     assertf((A)[sizeof(A) - 1] == '\0'); \
  372.   } \
  373. } while(0)
  374.  
  375. #else
  376.  
  377. #define strcatbuff strcat
  378. #define strncatbuff strncat
  379. #define strcpybuff strcpy
  380. #define strncpybuff strncpy
  381.  
  382. #endif
  383.  
  384. #endif
  385.  
  386. #endif
  387.  
  388. #ifdef __cplusplus
  389. }
  390. #endif
  391.  
  392. #endif
  393.